package ni;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import org.json.JSONObject;

public class UDPsender {
	
	private final DatagramSocket dataSocket;
    private final int destination_port;


	public UDPsender(int port, int destination_port) throws SocketException {
		this.destination_port = destination_port;
		this.dataSocket = new DatagramSocket(port);
	}



	public DatagramSocket getDataSocket() {
		return dataSocket;
	}

	public void sendMessage(Message message, InetAddress destAddress) {
		String messageString = new String(message.toString().getBytes());
		byte[] data = messageString.getBytes();
		DatagramPacket dp = new DatagramPacket(data, data.length, destAddress, destination_port);
        try {
			dataSocket.send(dp);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void close_socket_sender(){
		this.dataSocket.close();
	}
	
}
